﻿Imports System.Data.SqlClient
Imports System.Text

Namespace BLApostas

    Public Class BLApostas

        Dim DLApostas As New DataAcessLayer.clsApostas

        Public Function ValidarRegistar(ByVal username As String,
                                        ByVal displayname As String,
                                        ByVal email As String,
                                        ByVal password As String,
                                        ByVal confirmarpassword As String) As String
            Try
                Dim resultado As String = "Dados em falta/incorrectos;"
                Dim infoFalta As Boolean = False

                If username.Equals(String.Empty) Then

                    resultado = resultado & Space(1) & "UserName;"
                    infoFalta = True

                End If

                If displayname.Equals(String.Empty) Then

                    resultado = resultado & Space(1) & "displayname;"
                    infoFalta = True

                End If

                If email.Equals(String.Empty) Then

                    resultado = resultado & Space(1) & "email;"
                    infoFalta = True

                End If

                If password.Equals(String.Empty) Then

                    resultado = resultado & Space(1) & "password;"
                    infoFalta = True

                End If

                If confirmarpassword.Equals(String.Empty) Then

                    resultado = resultado & Space(1) & "confirmarpassword;"
                    infoFalta = True

                End If

                If infoFalta = False Then

                    resultado = String.Empty

                    If password.Equals(confirmarpassword) Then

                        resultado = CriarUtilizador(username, displayname, email, password)

                    Else

                        resultado = "Por favor confirme de novo a sua Password!"

                    End If

                End If

                Return resultado

            Catch ex As Exception

                Return "Ocorreu uma exepção!"

            End Try

        End Function

        Public Function CriarUtilizador(ByVal UserName As String,
                                         ByVal DisplayName As String,
                                         ByVal Email As String,
                                         ByVal Password As String) As String
            Try

                Dim verificado As Boolean = DLApostas.A_GET_VerificarUtilizador(UserName)

                Select Case verificado

                    Case verificado = Nothing

                        Return "Erro na criação do Utilizador!"

                    Case False

                        Return DLApostas.A_ADD_Utilizador(UserName, DisplayName, Email, Password)

                    Case True

                        Return "Utilizador já existe, escolha outro UserName!"

                    Case Else

                        Return "Ocorreu um erro, contacte o administrator!"
                End Select

            Catch ex As Exception

                Return "Ocorreu uma exepção!"
            End Try

        End Function

        Public Function AutenticarUtilizador(ByVal UserName As String, ByVal Password As String, ByRef DisplayName As String, ByRef IDUtilizador As Integer) As String

            Try

                Dim user As SqlDataReader = DLApostas.A_GET_UtilizadorPorUserName(UserName)

                Select Case user.HasRows

                    Case True

                        Dim _Password As String = String.Empty

                        user.Read()

                        DisplayName = user("DisplayName").ToString
                        IDUtilizador = user("IDUtilizador").ToString
                        _Password = user("Password").ToString
                        user.Close()

                        If Not Password = _Password Then

                            Return "Password Incorrecta!"
                        End If

                        Return "OK"

                    Case False

                        Return "Utilizador Inexistente!"

                    Case Else

                        Return "Ocorreu um erro, contacte o administrator!"

                End Select

            Catch ex As Exception

                Return "Ocorreu uma exepção!"
            End Try

        End Function

        Public Function ObterUtilizadorCampeonatos(ByVal IDUtilizador As Integer) As DataTable

            Return DLApostas.A_GET_UtilizadorCampeonatos(IDUtilizador)

        End Function

        Public Function ObterUtilizadorCampeonatoActivo(ByVal IDUtilizador As Integer) As Dictionary(Of String, Object)

            Dim dr As SqlDataReader
            Dim idic As New Dictionary(Of String, Object)

            dr = DLApostas.A_GET_UtilizadorCampeonatoActivo(IDUtilizador)

            If dr.Read Then

                Dim util As New BLUtilities.BLUtilities
                idic = util.DataReaderToDictionary(dr)

            End If
            dr.Close()

            Return idic

        End Function

        Public Function ObterCampeonatos() As DataTable

            Return DLApostas.A_GET_Campeonatos

        End Function

        Public Function ObterJornadaCampeonato(ByVal IDCampeonato As Integer, ByVal IDJornada As Integer) As DataTable

            Return DLApostas.A_GET_JornadaCampeonato(IDCampeonato, IDJornada)

        End Function

        Public Function ActivarCampeonato(ByVal IDUtilizador As Integer, ByVal IDCamepeonato As Integer) As String

            Return DLApostas.A_UPD_ActivarCampeonato(IDUtilizador, IDCamepeonato)

        End Function

#Region "Apostas"

        Public Function ValidarApostas(ByVal Aposta1 As String,
                                        ByVal Aposta2 As String,
                                        ByVal NumJogo As String) As String

            Try
                Dim sb As New StringBuilder

                If Aposta1.Equals(String.Empty) Then

                    sb.Append("<br />" & "Falta Apostar na Equipa 1 do Jogo nº " & NumJogo & "!")

                End If

                If Aposta2.Equals(String.Empty) Then

                    sb.Append("<br />" & "Falta Apostar na Equipa 2 do Jogo nº " & NumJogo & "!")

                End If

                Return sb.ToString

            Catch ex As Exception

                Return ex.Message.ToString
            End Try

        End Function

        Public Function Apostar(ByVal IDAposta As Integer,
                                ByVal IDUtilizador As Integer,
                                ByVal IDCampeonato As Integer,
                                ByVal IDJornada As Integer,
                                ByVal NumJogo As Integer,
                                ByVal Aposta1 As Integer,
                                ByVal Aposta2 As Integer) As String

            Dim sb As New StringBuilder

            Try

                Dim apostado As String = DLApostas.A_UPD_Aposta(IDAposta, IDCampeonato, IDUtilizador, IDJornada, NumJogo, Aposta1, Aposta2)

                Select Case apostado

                    Case "OK"

                        sb.Append("<br />" & "Aposta " & NumJogo & " Realizada com sucesso!")

                    Case Else

                        sb.Append("<br />" & "Ocorreu um erro, contacte o administrator!")
                End Select

                Return sb.ToString

            Catch ex As Exception

                sb.Append("<br />" & "Ocorreu uma excepção!" & ex.Message.ToString)
                Return sb.ToString

            End Try

        End Function

        Public Function ObterJornadas() As DataTable

            Return DLApostas.A_GET_Jornadas

        End Function

        Public Function ObterJornadaDescricao(ByVal IDUtilizador As Integer,
                                              ByVal IDJornada As Integer) As DataTable

            Dim dt As DataTable = DLApostas.A_GET_ApostasUtilizadorJornada(IDUtilizador, IDJornada)

            Return dt

        End Function

#End Region

    End Class

End Namespace

Namespace BLUtilities

    Public Class BLUtilities

        Public Function DataReaderToDictionary(ByVal dr As SqlDataReader) As Dictionary(Of String, Object)

            Try
                Dim idic As New Dictionary(Of String, Object)
                Dim nameReader As String = String.Empty
                Dim valueReader As String = String.Empty

                For i As Integer = 0 To dr.FieldCount - 1
                    Try
                        nameReader = dr.GetName(i)
                        If Not IsDBNull(dr(i)) Then
                            valueReader = dr(i)
                        Else
                            valueReader = String.Empty
                        End If

                        idic.Add(nameReader, valueReader)
                    Catch
                    End Try
                Next

                Return idic

            Catch ex As Exception

                Return Nothing
            End Try

        End Function

        Public Function DataTableToDictionary(ByVal dr As DataTable) As List(Of Dictionary(Of String, Object))

            Try
                Dim iDic As New Dictionary(Of String, Object)
                Dim iList As New List(Of Dictionary(Of String, Object))

                Dim nameReader As String = String.Empty
                Dim valueReader As String = String.Empty

                Dim rowsAux As Integer = 0
                Dim rows As Integer = dr.Rows.Count - 1

                While rowsAux <= rows

                    For index = 0 To dr.Columns.Count - 1

                        nameReader = dr.Columns(index).ColumnName
                        valueReader = dr.Rows(rowsAux).Item(index)

                        iDic.Add(nameReader, valueReader)
                    Next

                    iList.Add(iDic)

                    rowsAux += 1

                    iDic.Clear()

                End While

                Return iList

            Catch ex As Exception

                Return Nothing
            End Try

        End Function

    End Class

End Namespace

Namespace BLAtributes

    Public Class BLAtributes

        Public Class OperacoesApostas

            Public Const Visualizar As String = "VER"
            Public Const Apostar As String = "APOSTAR"

        End Class

        Public Class EstadoJogo

            Public Const Realizado As String = "Jogo já realizado!"
            Public Const PorRealizar As String = "Jogo ainda não realizado..."

        End Class

    End Class

End Namespace
